﻿using AdminStyem.Application.Command.traceabilityCommand;
using AdminStyem.Domain.Entity;
using AdminStyem.Repositories;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.traceabilityCommandHandler
{
    public class TraceabilityAddCommandHandler : IRequestHandler<TraceabilityAddCommand, APIResult<object>>
    {
        IBaseRespository<Traceability> BaseTraceability;
        IMapper mapper;




        public TraceabilityAddCommandHandler(IBaseRespository<Traceability> baseTraceability, IMapper mapper)
        {
            BaseTraceability = baseTraceability;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(TraceabilityAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> ret = new APIResult<object>();
            ret.Code = ResultCode.添加成功;
            ret.Message = ResultCode.添加成功.ToString();

            var traceability = mapper.Map<Traceability>(request);
         int i=BaseTraceability.add(traceability).Result;
            if (i<=0)
            {
                ret.Code = ResultCode.添加成功;
                ret.Message = ResultCode.添加成功.ToString();
                return Task.FromResult(ret);
            }
            return Task.FromResult(ret);

        }
    }
}
